package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Test;
import cn.wolfcode.crm.domain.Test;
import cn.wolfcode.crm.mapper.TestMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.TestQueryObject;
import cn.wolfcode.crm.service.ITestService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TestServiceImpl implements ITestService {
    @Autowired
    private TestMapper testMapper;
    @Override
    public void deleteByPrimaryKey(Long id) {
        testMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Test entity) {
        testMapper.insert(entity);
    }

    @Override
    public Test selectByPrimaryKey(Long id) {

        return testMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Test> selectAll() {
        return testMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Test entity) {
        testMapper.updateByPrimaryKey(entity);
    }

    public void changState(Long id) {
        testMapper.changState(id);
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer total = testMapper.query4Count(qo);
        if(total==0){
            return PageResult.PAGERESULT_EMPTY;
        }
        return new PageResult(total,testMapper.query4List(qo));
    }

    /**
     * 根据条件查询员工列表导出到exl
     *
     * @param qo
     * @return
     */
    @Override
    public Workbook exportXls(TestQueryObject qo) {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("考试编号");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("营销人员");
        row.createCell(3).setCellValue("QQ");
        row.createCell(4).setCellValue("电话");
        row.createCell(5).setCellValue("意向班级");
        row.createCell(6).setCellValue("考试时间");
        row.createCell(7).setCellValue("考试结果");
        row.createCell(8).setCellValue("备注");
        row.createCell(9).setCellValue("处理人");

        List<?> list = testMapper.query4List(qo);

        for (int i = 0; i < list.size(); i++) {
            try {
                Test test = (Test) list.get(i);
                //创建行
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(test.getId());
                row.createCell(1).setCellValue(test.getClient() != null ? test.getClient().getName() : "");
                row.createCell(2).setCellValue(test.getClient() != null ? test.getClient().getSaleman().getUsername() : "");
                row.createCell(3).setCellValue(test.getClient() != null ? test.getClient().getQq() : "");
                row.createCell(4).setCellValue(test.getClient() != null ? test.getClient().getTel() : "");
                row.createCell(5).setCellValue(test.getClient() != null ? test.getClient().getClz().getName() : "");
                row.createCell(6).setCellValue(test.getClient() != null ? test.getTestDate().toString() : "");
                row.createCell(7).setCellValue(test.getTestResult()? "通过" : "未通过");
                row.createCell(8).setCellValue(test.getRemark());
                row.createCell(9).setCellValue(test.getHandler() != null ? test.getHandler().getUsername() : "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return book;
    }


}
